%----------------------------------------------------------------------------------------
%   SETTINGS
%----------------------------------------------------------------------------------------

%\makeatletter % changes the catcode of @ to 11
%modify here packages internal commands
%\makeatother % changes the catcode of @ back to 12

\makeatletter

%-------------------------------------------
%   Add preinfo to header
%-------------------------------------------

\newcommand*{\preinfo}[1]{\def\@preinfo{#1}}

% as \quotewidth can't be changed, we define \quotecustomwidth
\newlength{\quotecustomwidth}
\setlength{\quotecustomwidth}{0.65\textwidth}% default value
% optional maketitle width to force a certain width (if set to 0pt, the width is calculated automatically)
\newlength{\makecvtitlenamewidth}
\setlength{\makecvtitlenamewidth}{0pt}% dummy value
\renewcommand*{\makecvtitle}{%
	{\hypersetup{urlcolor=color2}
	% recompute lengths (in case we are switching from letter to resume, or vice versa)
	\recomputecvlengths%
	% optional detailed information (pre-rendering)
	\def\phonesdetails{}%
	\collectionloop{phones}{% the key holds the phone type (=symbol command prefix), the item holds the number
		\protected@edef\phonesdetails{\phonesdetails\protect\makenewline\csname\collectionloopkey phonesymbol\endcsname\collectionloopitem}}%
	\def\socialsdetails{}%
	\collectionloop{socials}{% the key holds the social type (=symbol command prefix), the item holds the link
		\protected@edef\socialsdetails{\socialsdetails\protect\makenewline\csname\collectionloopkey socialsymbol\endcsname\collectionloopitem}}%
	\newbox{\makecvtitledetailsbox}%
	\savebox{\makecvtitledetailsbox}{%
		\addressfont\color{color2}%
		\begin{tabular}[b]{@{}r@{}}%
			\ifthenelse{\isundefined{\@preinfo}}{}{\makenewline{\@preinfo}}%
			\ifthenelse{\isundefined{\@addressstreet}}{}{\makenewline\addresssymbol\@addressstreet%
			\ifthenelse{\equal{\@addresscity}{}}{}{\makenewline\@addresscity}% if \addresstreet is defined, \addresscity and addresscountry will always be defined but could be empty
			\ifthenelse{\equal{\@addresscountry}{}}{}{\makenewline\@addresscountry}}%
			\phonesdetails% needs to be pre-rendered as loops and tabulars seem to conflict
			\ifthenelse{\isundefined{\@email}}{}{\makenewline\emailsymbol\emaillink{\@email}}%
			\ifthenelse{\isundefined{\@homepage}}{}{\makenewline\homepagesymbol\httplink{\@homepage}}%
			\socialsdetails% needs to be pre-rendered as loops and tabulars seem to conflict
			\ifthenelse{\isundefined{\@extrainfo}}{}{\makenewline\@extrainfo}%
		\end{tabular}
	}%
	% optional photo (pre-rendering)
	\newbox{\makecvtitlepicturebox}%
	\savebox{\makecvtitlepicturebox}{%
		\ifthenelse{\isundefined{\@photo}}%
			{}%
			{%
				\hspace*{\separatorcolumnwidth}%
				\color{color1}%
				\setlength{\fboxrule}{\@photoframewidth}%
				\ifdim\@photoframewidth=0pt%
					\setlength{\fboxsep}{0pt}\fi%
				\framebox{\includegraphics[width=\@photowidth]{\@photo}}%
			}%
	}%
	% name and title
	\newlength{\makecvtitledetailswidth}\settowidth{\makecvtitledetailswidth}{\usebox{\makecvtitledetailsbox}}%
	\newlength{\makecvtitlepicturewidth}\settowidth{\makecvtitlepicturewidth}{\usebox{\makecvtitlepicturebox}}%
	\ifthenelse{\lengthtest{\makecvtitlenamewidth=0pt}}% check for dummy value (equivalent to \ifdim\makecvtitlenamewidth=0pt)
		{\setlength{\makecvtitlenamewidth}{\textwidth-\makecvtitledetailswidth-\makecvtitlepicturewidth}}%
		{}%
	\begin{minipage}[b]{\makecvtitlenamewidth - 10mm}%
		\raggedright
		\titlestyle{\@title}\\
		\hfill\\
		\namestyle{\@firstname\ \@lastname}
	\end{minipage}%
	\hfill%
	% optional detailed information (rendering)
	\llap{\usebox{\makecvtitledetailsbox}}% \llap is used to suppress the width of the box, allowing overlap if the value of makecvtitlenamewidth is forced
	% optional photo (rendering)
	\usebox{\makecvtitlepicturebox}\\[2.5em]%
	% optional quote
	\ifthenelse{\isundefined{\@quote}}%
		{}%
		{{\centering\begin{minipage}{\quotecustomwidth}\vspace*{\quoteDeletedSpace}\centering\quotestyle{\@quote}\end{minipage}\\[2.5em]}}%
	\par% to avoid weird spacing bug at the first section if no blank line is left after \makecvtitle
	}
}

%-------------------------------------------
%   Change tllabelcventry label position
%-------------------------------------------

\renewcommand{\tllabelcventry}[9][color1]{%
	\tl@formatendyear{#3}
	\tl@formatstartyear{#2}
	\cventry{
		\tikz[baseline=0pt]{
			\fill [\tl@runningcolor] (0,0) rectangle (\hintscolumnwidth,\tl@runningwidth);
			\useasboundingbox (0,-1.5ex)
				rectangle (\hintscolumnwidth,1ex);
			\fill [#1] (0,0)
				++(\tl@startfraction*\hintscolumnwidth,0pt)
				rectangle (\tl@endfraction*\hintscolumnwidth,\tl@width);
			\node[#1,tl@singleyear,anchor=north] at (\tl@startfraction*\hintscolumnwidth/2+\tl@endfraction*\hintscolumnwidth/2,0pt) {#4};
		}
	}
	{#5}{#6}{#7}{#8}{#9}%
}


%-------------------------------------------
%   Define tldoublelabelcventry
%-------------------------------------------

\pgfkeys{
	/tldoublelabelcventry/.is family,
	/tldoublelabelcventry,
	default/.style={
		starta = 0,
		enda = 0,
		startb = 0,
		endb = 0,
		label = label,
	},
	starta/.estore in = \tlstarta,
	enda/.estore in = \tlenda,
	startb/.estore in = \tlstartb,
	endb/.estore in = \tlendb,
	label/.estore in = \tllabel,
}
\newcommand{\tldoublelabelcventry}[7][color1]{%
	\pgfkeys{/tldoublelabelcventry, default, #2}%
	\tl@formatstartyear{\tlstarta}
	\edef\tl@startfractiona{\tl@startfraction}
	\tl@formatendyear{\tlenda}
	\edef\tl@endfractiona{\tl@endfraction}
	\tl@formatstartyear{\tlstartb}
	\edef\tl@startfractionb{\tl@startfraction}
	\tl@formatendyear{\tlendb}
	\edef\tl@endfractionb{\tl@endfraction}
	\cventry{
		\tikz[baseline=0pt]{
			\fill [\tl@runningcolor] (0,0) rectangle (\hintscolumnwidth,\tl@runningwidth);
			\useasboundingbox (0,-1.5ex)
				rectangle (\hintscolumnwidth,1ex);
			\fill [#1] (0,0)
				++(\tl@startfractiona*\hintscolumnwidth,0pt)
				rectangle (\tl@endfractiona*\hintscolumnwidth,\tl@width);
			\fill [#1] (0,0)
				++(\tl@startfractionb*\hintscolumnwidth,0pt)
				rectangle (\tl@endfractionb*\hintscolumnwidth,\tl@width);
			\pgfmathsetmacro\pos{(\tl@startfractiona+\tl@startfractionb+\tl@endfractiona+\tl@endfractionb)/4*\hintscolumnwidth}
			\node[#1,tl@singleyear,anchor=north] at (\pos pt,0pt) {\tllabel};
		}
	}
	{#3}{#4}{#5}{#6}{#7}%
}

\makeatother

%-------------------------------------------
%   Theme related colors
%-------------------------------------------

% color0      main default color, normally left to black
% color1      primary scheme color
% color2      secondary scheme color

%----------------------------------------
%   General configuration
%----------------------------------------

% reduce underscores size
\renewcommand{\_}{\textscale{.5}{\textunderscore}}

%-------------------------------------------
%   Lengths modifications
%-------------------------------------------

\AtBeginDocument{%

\setlength{\quotecustomwidth}{0.85\textwidth}

%\setlength{\quotewidth}{0.65\textwidth}
%\setlength{\separatorcolumnwidth}{0.025\textwidth}
%\setlength{\maincolumnwidth}{\textwidth}
%\settowidth{\listitemsymbolwidth}{\listitemsymbol}
%\sethintscolumnlength{<length>} % <length> is the desired length in a unit LaTeX understands
%\sethintscolumntowidth{<string>} % where <string> is a string of the desired length (usually, the longest string that has to appear in the column)

}

%-------------------------------------------
%   Theme font and style redefinition
%-------------------------------------------

\AtBeginDocument{%

% fonts
\renewcommand*{\titlefont}{\fontsize{32}{14}\mdseries\upshape}
\renewcommand*{\namefont}{\fontsize{19}{14}\mdseries\upshape}
%\renewcommand*{\addressfont}{\normalsize\mdseries\upshape}
%\renewcommand*{\quotefont}{\large\slshape}
%\renewcommand*{\sectionfont}{\Large\bfseries\upshape}
%\renewcommand*{\subsectionfont}{\large\upshape\fontseries{sb}\selectfont}
%\renewcommand*{\hintfont}{\bfseries}

% styles
\renewcommand*{\titlestyle}[1]{{\titlefont\textcolor{color1}{#1}}}
\renewcommand*{\namestyle}[1]{{\namefont\textcolor{color2!85}{#1}}}
%\renewcommand*{\addressstyle}[1]{{\addressfont\textcolor{color2}{#1}}}
%\renewcommand*{\quotestyle}[1]{{\quotefont\textcolor{color1}{#1}}}
%\renewcommand*{\sectionstyle}[1]{{\sectionfont\textcolor{color1}{#1}}}
%\renewcommand*{\subsectionstyle}[1]{{\subsectionfont\textcolor{color1}{#1}}}
%\renewcommand*{\hintstyle}[1]{{\hintfont\textcolor{color0}{#1}}}

}

%-------------------------------------------
%   hyperref colors
%-------------------------------------------

\AtBeginDocument{%

\hypersetup{
	colorlinks=true,
	%linkcolor=red,
	%anchorcolor=black,
	citecolor=green,
	%filecolor=cyan,
	%menucolor=red,
	urlcolor=color1,
	linktoc=all
}

}

%-------------------------------------------
%   Symbols from FontAwesome
%-------------------------------------------

\AtBeginDocument{%

\renewcommand*{\addresssymbol}       {\faMapMarker\ }
\renewcommand*{\mobilephonesymbol}   {\faMobile\ }
\renewcommand*{\fixedphonesymbol}    {\foPhone\ }
\renewcommand*{\faxphonesymbol}      {\faFax\ }
\renewcommand*{\emailsymbol}         {\faEnvelopeO\ }
\renewcommand*{\homepagesymbol}      {\faGlobe\ }
\renewcommand*{\linkedinsocialsymbol}{\faLinkedin\ }
\renewcommand*{\twittersocialsymbol} {\faTwitter\ }
\renewcommand*{\githubsocialsymbol}  {\faGithub\ }

}

%-------------------------------------------
%   Commands definitions
%-------------------------------------------

% Used to indicate the level in the languages section
\newcommand{\cvskill}[2]{%
\textcolor{color2}{\textbf{#1}}
\foreach \x in {1,...,5}{%
	\space{\ifnumgreater{\x}{#2}{\color{color1!30}}{\color{color1}}\faCircle}}%
}

\newcommand{\cvitemcom}[2]{%
	\cvitemwithcomment{}{\listitemsymbol #1}{#2}%
}

% Colored href
\newcommand{\Chref}[3][blue]{\href{#2}{\color{#1}{#3}}}%

% Space to delete between the sections
\newcommand{\deletedSpace}{0mm}

% Space to delete before the quote
\newcommand{\quoteDeletedSpace}{0mm}

% C++
\newcommand{\Cpp}{\texorpdfstring{{C\nolinebreak[4]\hspace{-.05em}\raisebox{.4ex}{\smaller\smaller\bfseries ++}}}{C++}}

%-------------------------------------------
%   Age computation
%-------------------------------------------

\newcounter{birthday}
\newcounter{today}
\setmydatenumber{birthday}{1996}{06}{19}
\setmydatenumber{today}{\the\year}{\the\month}{\the\day}
\FPsub\result{\thetoday}{\thebirthday}
\FPdiv\myage{\result}{365.2425}
\FPtrunc\myage{\myage}{0}

%-------------------------------------------
%   Bibliography / publications
%-------------------------------------------

\bibliography{publications}

%-------------------------------------------
%   ModernCV theme
%-------------------------------------------

\moderncvtheme[blue]{banking}
%\moderncvtheme[blue]{classic}
%\renewcommand{\familydefault}{\sfdefault}
\nopagenumbers{}
